home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / editor / snmp_0_1.zip / snmp-0.1 / aka / snmp / SnmpObject.java < prev    next >
Text File  |  1997-06-09  |  4KB  |  188 lines

  1. /*
  2. Snmp Library
  3. Copyright (C) 1997 Alex Kowalenko Associates Pty Ltd. All rights reserved.
  4.  
  5. This software maybe be free distributed, any any form, without fee, 
  6. but may not be modified in any way without express permission of 
  7. the directors of Alex Kowalenko Associates Pty Ltd. 
  8.  
  9. Alex Kowalenko Associates Pty Ltd makes no representations or
  10. warranties about the suitabililty of the software, not even the
  11. implied warranty of merchantability or fitness for any particular
  12. purpose.    
  13. */
  14.  
  15. package aka.snmp;
  16.  
  17. /**
  18.  * SNMP Object, as defined in the MIB
  19.  * @version     $Id: SnmpObject.java,v 1.7 1997/05/21 10:09:56 alex Exp $
  20.  * @author      Alex Kowalenko
  21.  */
  22.  
  23. import java.io.*;
  24.  
  25. public class  SnmpObject implements BERSerializable, Serializable {
  26.   
  27.   private String _name;
  28.   private ObjectId _id;
  29.   private boolean _node;
  30.   private Type _type;
  31.   private Access _access;
  32.   private Status _status;
  33.  
  34.     static SnmpObject Void = new SnmpObject("Void", "XXX");
  35.   
  36. /**
  37.  * Constructor, creates SNMP Object with name, and an id (in the form of a
  38.  * string).
  39.  */
  40.     
  41.   public SnmpObject(String name, String id) {
  42.       _name = name;
  43.       _id = new ObjectId(id);
  44.       _node = true;
  45.       _type = Type.make("NULL");
  46.       _access = new Access(Access.VOID);
  47.       _status = new Status(Status.VOID);
  48.   };
  49.  
  50. /**
  51.  * Constructor, creates SNMP Object with name, and an id.
  52.  */
  53.  
  54.   public SnmpObject(String name, ObjectId id) {
  55.       _name = name;
  56.       _id = id;
  57.       _node = true;
  58.       _type = Type.make("NULL");
  59.       _access = new Access(Access.VOID);
  60.       _status = new Status(Status.VOID);
  61.   };
  62.  
  63. /**
  64.  * Creates Snmp Object with name, ID (in the form of ObjectId), type, SNMP 
  65.  * ACCESS, and SNMP Status.  This creates a complete Snmp Object.
  66.  */
  67.  
  68.   public SnmpObject(String name, 
  69.             ObjectId id,
  70.             String typeName, 
  71.             Access access, 
  72.             Status status) {
  73.       _name = name;
  74.       _id = id;
  75.       _node = false;
  76.       _type = Type.make(typeName);
  77.       _access = access;
  78.       _status = status;
  79.   };
  80.  
  81. /** 
  82.  * This creates a SnmpObject from a byte representation, Used in
  83.  * BER serialization of the objects.
  84.  */
  85.  
  86.     SnmpObject(ByteBuffer buffer) throws SnmpPDUException {
  87.     if(buffer.size() == 0)
  88.         throw new SnmpPDUException("No objects left");
  89.     
  90.     // SEQUENCE | CONSTRUCTOR
  91.     if(!(buffer.byteAt(0) 
  92.          == (byte) (ASN.SEQUENCE|ASN.CONSTRUCTOR))) {
  93.         throw new SnmpPDUException("Incorrect coding");
  94.     };
  95.     buffer.removeBeginning(1);
  96.  
  97.     // Get length
  98.     int length = ASN.getLength(buffer);// Total length of the packet.
  99.         
  100.         // Parse Name
  101.     Type t = Type.parse(buffer);
  102.     _name = ((TypeString)t).value();
  103.         
  104.     // Parse Id
  105.     t = Type.parse(buffer);
  106.     _id = (ObjectId) t;
  107.  
  108.     // Parse _node
  109.     t = Type.parse(buffer);
  110.     _node = ((TypeInt)t).value() == 1;
  111.     
  112.     // Parse _type
  113.     t = Type.parse(buffer);
  114.     _type = Type.make(((TypeString)t).value());
  115.  
  116.     // Parse _access
  117.     t = Type.parse(buffer);
  118.     _access = new Access(((TypeInt)t).value());
  119.  
  120.     // Parse _status
  121.     t = Type.parse(buffer);
  122.     _status = new Status(((TypeInt)t).value());
  123.     return;
  124.     };
  125.  
  126. /**
  127.  * Serialize
  128.  */
  129.  
  130.     public ByteBuffer BERSerialize() {
  131.     ByteBuffer block = new ByteBuffer();
  132.     Type t = new TypeString(_name);
  133.     block.append(t.BERSerialize());
  134.     block.append(_id.BERSerialize());
  135.     t = new TypeInt( _node ? 1 : 0);
  136.     block.append(t.BERSerialize());
  137.     t = new TypeString( _type.typeName());
  138.     block.append(t.BERSerialize());
  139.     t = new TypeInt( _access.value());
  140.     block.append(t.BERSerialize());
  141.     t = new TypeInt( _status.value());
  142.     block.append(t.BERSerialize());
  143.  
  144.     ByteBuffer result = new ByteBuffer();
  145.     result.append((byte) (ASN.SEQUENCE | ASN.CONSTRUCTOR));
  146.     result.append(ASN.buildLength(block.size()));
  147.     result.append(block);
  148.     return result;
  149.     }; 
  150.   
  151.     
  152.   public String name() {
  153.     return _name;
  154.     }
  155.  
  156.   public ObjectId id() {
  157.     return _id;
  158.     }
  159.  
  160.   public Access access() {
  161.     return _access;
  162.     }
  163.  
  164.   public Status status() {
  165.     return _status;
  166.     }
  167.  
  168.   public Type type() {
  169.     return _type;
  170.     };
  171.  
  172.   public boolean isNode() {
  173.     return _node;
  174.     }
  175.  
  176.   public String toString() {
  177.       String result = "{" + _name + ", " + _id;
  178.       if(_node) {
  179.       result += "}";
  180.       } else {
  181.       result += ", " + _type.typeName() + ", " + 
  182.           _access + ", " + _status + "}";
  183.       }
  184.       return result;
  185.   }
  186.  
  187. };
  188.